<?php

namespace pay\modules\frontend\models;

use Yii;

/**
 * This is the model class for table "pay_refund_order_log".
 *
 * @property int $id
 * @property string $pay_order_sn 支付单号
 * @property string $refund_order_sn 退款单号
 * @property string $pay_user 退款用户
 * @property int $refund_fee 退款金额
 * @property string $refund_ip 退款ip
 * @property string $refund_params 退款参数
 * @property string $result_params 退款返回值
 * @property string $create_time 创建时间
 */
class RefundOrderLog extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'pay_refund_order_log';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['pay_order_sn', 'refund_order_sn', 'pay_user', 'refund_fee', 'refund_params'], 'required'],
            [['refund_fee'], 'integer'],
            [['create_time'], 'safe'],
            [['pay_order_sn', 'refund_order_sn', 'pay_user', 'refund_ip'], 'string', 'max' => 32],
            [['refund_params', 'result_params'], 'string', 'max' => 500],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'pay_order_sn' => 'Pay Order Sn',
            'refund_order_sn' => 'Refund Order Sn',
            'pay_user' => 'Pay User',
            'refund_fee' => 'Refund Fee',
            'refund_ip' => 'Refund Ip',
            'refund_params' => 'Refund Params',
            'result_params' => 'Result Params',
            'create_time' => 'Create Time',
        ];
    }

    public function addLog($data)
    {
        $params = [
            'RefundOrderLog' => [
                'pay_order_sn' => $data['paySerialId'],
                'refund_order_sn' => $data['refundUserSerial'],
                'pay_user' => $data['payUserId'],
                'refund_fee' => $data['refundFee'],
                'refund_ip' => Yii::$app->request->userIP,
                'refund_params' => json_encode($data,JSON_UNESCAPED_UNICODE),
                'create_time' => date("Y-m-d H:i:s"),
            ]
        ];
        if ($this->load($params) and $this->validate()){
            if ($this->save(0)){
                return $this->getPrimaryKey();
            }
        }
        return false;
    }


}
